﻿namespace DataMorph.Web.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Runtime.Serialization;

    using DataMorph.Common;
    using DataMorph.Common.Entities;

    [DataContract]
    public class GenerationParametersModel
    {
        [Required]
        [DataMember(IsRequired = true)]
        public string ClientId { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public string DataSetName { get; set; }

        public DateTime TimeRequested { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public string StorageAccountName { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public string StorageAccountKey { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public int NumberOfFiles { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public DateTime StartDate { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public int NumberOfDays { get; set; }

        [Required]
        [Range(1 * 1000 * 1000, 4 * 1000 * 1000)]
        [DataMember(IsRequired = true)]
        public int MinFileSize { get; set; }

        [Required]
        [Range(1 * 1000 * 1000, 4 * 1000 * 1000)]
        [DataMember(IsRequired = true)]
        public int MaxFileSize { get; set; }

        [Required]
        [DataMember(IsRequired = true)]
        public int NumberOfTags { get; set; }

        public int MaxNumberOfTagsPerFile { get; set; }

        public static GenerationParametersModel FromDataSetEntity(DataSetEntity entity)
        {
            if (entity == null)
            {
                return null;
            }

            return new GenerationParametersModel
                {
                    DataSetName = entity.Name,
                    StorageAccountName = entity.StorageAccountName,
                    StorageAccountKey = entity.StorageAccountKey,
                    NumberOfTags = entity.NumberOfTags,
                    NumberOfFiles = entity.NumberOfFiles,
                    StartDate = entity.StartDateTime,
                    NumberOfDays = entity.NumberOfDays,
                    MinFileSize = entity.MinFileSize,
                    MaxFileSize = entity.MaxFileSize,
                    TimeRequested = entity.Requested
                };
        }

        public GenerationParameters ToGenerationParameters()
        {
            return new GenerationParameters
            {
                Name = this.DataSetName,
                StorageAccountName = this.StorageAccountName,
                StorageAccountKey = this.StorageAccountKey,
                NumberOfTags = this.NumberOfTags,
                NumberOfFiles = this.NumberOfFiles,
                StartDateTime = this.StartDate,
                NumberOfDays = this.NumberOfDays,
                MinFileSize = this.MinFileSize,
                MaxFileSize = this.MaxFileSize,
                Requested = this.TimeRequested
            };
        }
    }
}